package cn.hzking.gjmall.delegates.main;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mikhaellopez.circularimageview.CircularImageView;

import butterknife.Bind;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.my.AddAdressActivity;
import cn.hzking.gjmall.activitys.my.AddressListActivity;
import cn.hzking.gjmall.activitys.my.MyCollectActivity;
import cn.hzking.gjmall.activitys.my.MyCouponActivity;
import cn.hzking.gjmall.activitys.my.MyOrderActivity;
import cn.hzking.gjmall.activitys.my.SettingActivity;
import cn.hzking.gjmall.base.BaseDelegate;
import cn.hzking.gjmall.bean.MyInfoBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyDelegate extends BaseDelegate {
    @Bind(R.id.my_headimage)
    CircularImageView myHeadimage; //圆形头像
    @Bind(R.id.my_nickname)
    TextView myNickname;  //昵称
    @Bind(R.id.my_vip)
    TextView myVip;  //vip编号
    @Bind(R.id.my_item_totleorder)
    RelativeLayout myItemTotleorder; //全部订单
    @Bind(R.id.my_waitpay)
    LinearLayout myWaitpay; //待付款
    @Bind(R.id.my_waitsend)
    LinearLayout myWaitsend;//待发货
    @Bind(R.id.my_waitreceive)
    LinearLayout myWaitreceive; //待收货
    @Bind(R.id.my_backmoney)
    LinearLayout myBackmoney; //售后/退款
    @Bind(R.id.my_item_realname)
    RelativeLayout myItemRealname;//实名认证
    @Bind(R.id.my_item_jifen)
    RelativeLayout myItemJifen; //我的积分布局
    @Bind(R.id.my_item_card)
    RelativeLayout myItemCard; //卡券包
    @Bind(R.id.my_item_soucan)
    RelativeLayout myItemSoucan; //我的收藏
    @Bind(R.id.my_item_address)
    RelativeLayout myItemAddress; //收货地址
    @Bind(R.id.rl_setting)
    RelativeLayout settingRl; //设置
    @Bind(R.id.tv_jifen) TextView jifenTv;
    @Bind(R.id.tv_collect) TextView collectTv;
    @Bind(R.id.tv_waitpaynum) TextView waitpaynumTv;
    @Bind(R.id.tv_waitsendnum) TextView waitsendnumTv;
    @Bind(R.id.tv_waitreceivenum) TextView waitreceivenumTv;
    @Bind(R.id.tv_refundnum) TextView refundnumTv;
    @Bind(R.id.rl_waitpay) RelativeLayout waitpayRl;
    @Bind(R.id.rl_waitsend) RelativeLayout waitsendRl;
    @Bind(R.id.rl_waitreceive) RelativeLayout waitreceiveRl;
    @Bind(R.id.rl_refund) RelativeLayout refundRl;


    public MyDelegate() {
        // Required empty public constructor
    }


    @Override
    public Object setLayout() {
        return R.layout.fragment_my_delegate;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
       initListener();

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initData();
    }

    private void initData() {
        RetrofitFactory.getInstant().API()
                .myInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MyInfoBean>(_mActivity,false) {
                    @Override
                    protected void onSuccees(MyInfoBean myInfoBean) throws Exception {
                        if(myInfoBean.getData().getBaseInfo()!=null){
                            setData(myInfoBean.getData().getBaseInfo());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                    }

                    @Override
                    protected void onCodeError(MyInfoBean myInfoBean) throws Exception {
                        super.onCodeError(myInfoBean);
                        UIUtils.showToastShort(_mActivity,myInfoBean.getErrorMsg());
                    }
                });
    }

    private void setData(MyInfoBean.DataBean.BaseInfoBean baseInfoBean) {
        jifenTv.setText(""+baseInfoBean.getJifen());
        collectTv.setText(""+baseInfoBean.getFavoriteNumber());
        myVip.setText("VIP: "+baseInfoBean.getVipNumber());
        myNickname.setText(baseInfoBean.getNikeName());
        if (baseInfoBean.getWaitPayCount()!=0){
            waitpayRl.setVisibility(View.VISIBLE);
            if(baseInfoBean.getWaitPayCount()>99){
                waitpaynumTv.setText("99");
            }else {
                waitpaynumTv.setText(""+baseInfoBean.getWaitPayCount());
            }
        }else {
            waitpayRl.setVisibility(View.GONE);
        }
        if(baseInfoBean.getAlreadySendCount()!=0){
            waitreceiveRl.setVisibility(View.VISIBLE);
            if(baseInfoBean.getAlreadySendCount()>99){
                waitreceivenumTv.setText("99");
            }else {
                waitreceivenumTv.setText(""+baseInfoBean.getAlreadySendCount());
            }

        }else {
            waitreceiveRl.setVisibility(View.GONE);
        }
        if(baseInfoBean.getWaitSendCount()!=0){
            waitsendRl.setVisibility(View.VISIBLE);
            if(baseInfoBean.getWaitSendCount()>99){
                waitsendnumTv.setText("99");
            }else {
                waitsendnumTv.setText(""+baseInfoBean.getWaitSendCount());
            }
        }else {
            waitsendRl.setVisibility(View.GONE);
        }

        if(!_mActivity.isFinishing()){
            Glide.with(_mActivity).load(baseInfoBean.getHeadImage()).into(myHeadimage);
        }
    }

    private void initListener() {
        //全部订单
        myItemTotleorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoOrderActivity(0);
            }
        });

        //待付款
        myWaitpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoOrderActivity(1);
            }
        });
        //待发货
        myWaitsend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoOrderActivity(2);
            }
        });
        //待收货
        myWaitreceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoOrderActivity(3);
            }
        });
        //退款/售后
        myBackmoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoOrderActivity(5);
            }
        });
        myItemSoucan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(_mActivity, MyCollectActivity.class);
                startActivity(intent);
            }
        });
        myItemAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(_mActivity, AddressListActivity.class);
                startActivity(intent);
            }
        });
        settingRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(_mActivity, SettingActivity.class));
            }
        });
        myItemCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(_mActivity, MyCouponActivity.class));
            }
        });
    }


    private void gotoOrderActivity(int fragmentid){
        Intent intent = new Intent(_mActivity, MyOrderActivity.class);
        intent.putExtra("fragmentId",fragmentid);
        startActivity(intent);
    }
}
